var userId = getQueryString("userId");
$(function () {
    $("#jqGrid").Grid({
        url: '../annexfile/list?knowId=' + userId,
        colModel: [
            // {label: '删除标识', name: 'delFlag', index: 'DEL_FLAG', width: 80},
            {label: '附件名称', name: 'fileName', index: 'FILE_NAME', width: 80},
            {label: '上传路径', name: 'url', index: 'URL', width: 100},
            {
                label: '文件类型', name: 'fileType', index: 'FILE_TYPE', width: 40, formatter: function (value) {
                    return sysEnums.KNOW_TYPE[value];
                }
            },
            {
                label: '模块类型', name: 'type', index: 'TYPE', width: 40, formatter: function (value) {
                    return sysEnums.KNOW_TYPE3[value];
                }
            },
            {
                label: '上传日期', name: 'uploadDate', index: 'UPLOAD_DATE', width: 40, formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd');
                }
            },
            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
        ]
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        annexFile: {},
        ruleValidate: {
            fileName: [
                {required: true, message: '附件名称不能为空', trigger: 'blur'}
            ],
            url: [
                {required: true, message: '附件不能为空', trigger: 'blur'}
            ]
        },
        q: {
            fileName: ''
        },
        picUrl: "",
        moduleTypeList: enumsToList('KNOW_TYPE3'), //模块类型
        knowTypeList: enumsToList('KNOW_TYPE') //知识归类
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.annexFile = {id: '', delFlag: 1, type: 1, fileType: 1};
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            if (vm.annexFile.id === (null || '')) {
                Ajax.request({
                    url: "../annexfile/saveUser/" + userId,
                    params: JSON.stringify(vm.annexFile),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function (r) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            } else {
                Ajax.request({
                    url: "../annexfile/update",
                    params: JSON.stringify(vm.annexFile),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function (r) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            }
        },
        del: function (event) {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }
            vm.getInfo(ids);
            confirm('确定要删除选中的记录？', function () {
                vm.annexFile.delFlag = 0;
                Ajax.request({
                    url: "../annexfile/update",
                    params: JSON.stringify(vm.annexFile),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function (r) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
                /*Ajax.request({
                    url: "../annexfile/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });*/
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../annexfile/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.annexFile = r.annexFile;
                    var url = vm.annexFile.url;
                    if (url) vm.picUrl = url.substring((url.lastIndexOf("\/") + 1), url.length);
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'fileName': vm.q.fileName},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                fileName: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        handleFormatError: function (file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请上传格式正确的图片。'
            });
        },
        handleMaxSize: function (file) {
            this.$Notice.warning({
                title: '超出文件大小限制',
                desc: '文件 ' + file.name + ' 太大，不能超过 2M。'
            });
        },
        handleSuccess: function (res, file) {
            this.$Loading.finish();
            iview.Message.success('文件上传成功');
            vm.annexFile.url = file.response.url;
            var url = vm.annexFile.url;
            if (url) vm.picUrl = url.substring((url.lastIndexOf("\/") + 1), url.length);
        },
        eyeFile: function (url) {
            if (url) {
                window.open(url);
            } else {
                alert('还未上传文件');
            }
        }
    }
});